package com.longrise.android.upload_offline.catlineplay;


import android.text.TextUtils;

/**
 * Created by sujizhong on 16/4/11.
 */
public class CatLineUtil {

    private static final int ERROR_POSITION = 1000;

    public static boolean IS_CUTLINE_PLAY = true;   //是否支持断点播放  false不支持

    /**
     * @param path 该视频唯一标示
     * @return 获得上次播放进度
     */
    public static int getCutCurrentPos(String path) {
        int mResumePos = 0;
        if (!IS_CUTLINE_PLAY || TextUtils.isEmpty(path)) {
            return mResumePos;
        }
        try {
            mResumePos = BBRedis.getPosition(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mResumePos;
    }

    public static int getCutDuration(String path) {
        int duration = -1;
        if (!IS_CUTLINE_PLAY) {
            return duration;
        }
        try {
            duration = BBRedis.getDuration(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return duration;
    }

    /**
     * @param path 该视频唯一标示
     *             移除该视频保存进度
     */
    public static void removeCutCurrentPos(String path) {
        if (!IS_CUTLINE_PLAY || TextUtils.isEmpty(path)) {
            return;
        }
        try {
            //播放完成之后
            BBRedis.removePosition(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param current  当前进度
     * @param duartion 总时长
     * @param path     该视频唯一标示 身份证号+课程id+配置路径
     *                 添加该视频当前进度 与 总时长
     */
    public static void addCutCurrentPos(int current, int duartion, String path) {
        if (!IS_CUTLINE_PLAY || TextUtils.isEmpty(path)) {
            return;
        }
        try {
            //进度条的单位为s 所以误差为1s
            if (current <= duartion - ERROR_POSITION && current >= ERROR_POSITION) {
                BBRedis.addPosition(path, current, duartion);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
